package org.gl.ui.manger;

import org.gl.android.base.R;
import org.gl.data.DataSources;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;


@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class BaseFragment extends Fragment implements LoadStatus,ActivityEventHandlerI
		{
	public View view;
	public DataSources ds;
	public String TAG;
	public Context context;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ds = new DataSources();
		TAG = this.getClass().getSimpleName();
		context = getActivity();
	}

	@Override
	public void loading() {
		showProgessView(true, "loading...");

	}

	public void receiveMessage(Object message) {

	}

	private void showProgessView(final boolean show, String message) {
		if (view != null) {
			final View loadView = view.findViewById(R.id.loading);
			if (loadView != null) {
				TextView tv = (TextView) loadView.findViewById(R.id.loading_tv);
				if ((tv) != null && message != null) {
					tv.setText(message);
				}
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
					int shortAnimTime = 1500;

					loadView.setVisibility(View.VISIBLE);
					loadView.animate().setDuration(shortAnimTime)
							.alpha(show ? 1 : 0)
							.setListener(new AnimatorListenerAdapter() {
								@Override
								public void onAnimationEnd(Animator animation) {
									loadView.setVisibility(show ? View.VISIBLE
											: View.GONE);
								}
							});

				} else {
					loadView.setVisibility(show ? View.VISIBLE : View.GONE);
				}
			}
		}
	}

	@Override
	public void loadFailed(String message) {
		showProgessView(true, message);
	}

	@Override
	public void loadSuccessed() {
		showProgessView(false, null);

	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	@Override
	public boolean onBackPressed() {
		return false;
	}
}
